﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Alphabet
{
	public class Operator
	{
		Symbols hands;
		Symbols legs;
		Symbols symbols;

		public int leftHands, rightHands, both, leftLegs, rightLegs = 0;

		string leftHand, rightHand, bothHands, leftLeg, rightLeg;

		int gap = 7;

		public Operator()
		{
			ReloadResources();
		}

		public string GetValue()
		{
			string result = symbols.GetRandomSymbol();

			if (result == leftHand)
			{
				result += rightHand;
				rightHands++;
			}
			else
				if (result == rightHand)
				{
					result += leftHand;
					leftHands++;
				}
				else
					result += GetNextHand();
			
			if (AppConfig.AppSettings.EnableLegs)
				result += GetNextLeg();

			return result;
		}

		public void ReloadResources()
		{
			hands = new Symbols(Strings.Hands);
			legs = new Symbols(Strings.Legs);
			symbols = new Symbols(Strings.Symbols);

			string allHands = hands.GetAllSymbols;
			leftHand = allHands.Substring(0, 1);
			rightHand = allHands.Substring(1, 1);
			bothHands = allHands.Substring(2, 1);

			string allLegs = legs.GetAllSymbols;
			leftLeg = allLegs.Substring(0, 1);
			rightLeg = allLegs.Substring(1, 1);

		}

		private string GetNextHand()
		{
			string hand = hands.GetRandomSymbol();

			if (hand == leftHand)
			{
				if ((leftHands > rightHands + gap))
				{
					rightHands++;
					return rightHand;
				}

				leftHands++;
				return leftHand;
			}

			if (hand == rightHand)
			{
				if ((rightHands > leftHands + gap))
				{
					leftHands++;
					return leftHand;
				}

				rightHands++;
				return rightHand;
			}

			both++;
			return bothHands;
		}

		private string GetNextLeg()
		{
			string leg = legs.GetRandomSymbol();

			if (leg == leftLeg)
			{
				if ((leftLegs > rightLegs + gap))
				{
					rightLegs++;
					return rightLeg;
				}

				leftLegs++;
				return leftLeg;
			}

			if (leg == rightLeg)
			{
				if ((rightLegs > leftLegs + gap))
				{
					leftLegs++;
					return leftLeg;
				}
			}

			rightLegs++;
			return rightLeg;
		}
	}
}